/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
// DataBool.cpp: implementation of the CDataBool class.
//
//////////////////////////////////////////////////////////////////////

#include <ctype.h>
#include "ssi_databool.h"
#include "ssi_dataint.h"
#include "ssi_databool.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SSI_SCRIPTDLL_API SSI_INT64 CDataBool::sType = 0;


CDataBool::CDataBool()
{
  Data = 0;
	if(!sType){
		sType = 1;
		sType = Factory_RegisterData((CData*)new CDataBool());
	}

	Type = sType;
	DataName = "bool";
}

CDataBool::~CDataBool()
{

}

SSI_BOOL CDataBool::CopyData(CData *Other)
{
	if((Type != Other->Type) && (Other->Type != CDataInt::GetRegisteredType()))  return 0;
  if(Other == SSI_NULL) return 0;
	if(Type == CDataInt::GetRegisteredType())
  {
    CDataInt* Temp = (CDataInt*)Other;
    if(Temp->Data == 0)
    {
      Data = 0;
    }
    else
    {
      Data = 1;
    }
  }
  else
  {
    CDataBool* Temp = (CDataBool*)Other;
    Data = Temp->Data;
  }
  return 1;
}

SSI_BOOL CDataBool::DataFromString(const String& sData)
{
  SSI_INT64 NumFields = 0;
	SSI_INT64 ctr = 0;
	SSI_CHAR c;

  if(sData == "")
  {
    Data = 0;
    return 0;
  }
  else if(sData == sTRUE)
  {
    Data = 1;
    return 1;
  }
  else if(sData == sFALSE)
  {
    Data = 0;
    return 1;
  }
	else{
		for(ctr = 0; ctr < sData.GetLength(); ctr++){
			c = sData[ctr];
			if(!isdigit(c)){
				if((c != '-') && (c != '+')){
					return 0;
				}
			}
		}
		sscanf(sData.Str(), "%d", &Data);
		if(Data != 0){
			Data = 1;
		}
		return 1;
	}
  return 0;
}

String CDataBool::GetString()
{
  String Ret;
  if(Data)
  {
    Ret = sTRUE;
  }
  else
  {
    Ret = sFALSE;
  }
  return Ret;
}

SSI_INT64 CDataBool::SizeOf()
{
  return sizeof(Data);
}

SSI_BOOL CDataBool::boolean()
{
  if(Data) return 1;
  return 0;
}

SSI_INT64 CDataBool::GetRegisteredType(void)
{
	if(!sType){
		CDataBool aData;
	}
	return sType;
}

CData* CDataBool::operator_plus(CData* InData, OpInfoT* Info)
{
	if(InData->GetType() != Type) return SSI_NULL;
	Data+=((CDataBool*)InData)->Data;
	if(Data!=0) Data = 1;
	return (CData*)this;
}

CData* CDataBool::operator_minus(CData* InData, OpInfoT* Info)
{
	if(InData->GetType() != Type) return SSI_NULL;
	Data = Data - ((CDataBool*)InData)->Data;
	if(Data!=0) Data = 1;
	return (CData*)this;
}

CData* CDataBool::operator_bit_and(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data = Data & ((CDataBool*)InData)->Data;
  }
	return (CData*)this;
}

CData* CDataBool::operator_bit_or(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data = Data | ((CDataBool*)InData)->Data;
  }
	return (CData*)this;
}

CData* CDataBool::operator_equal(CData* InData, OpInfoT* Info)
{
	if(InData->GetType() != Type) return SSI_NULL;
	Data = ((CDataBool*)InData)->Data;
	return (CData*)this;
}

CStatus CDataBool::operator_compare(CData* InData, SSI_INT64& Result)
{
  if(InData->GetType() != Type){
    return CData::operator_compare(InData, Result);
  }
	CDataBool* In = (CDataBool*)InData;
	if(Data < In->Data){
		Result = -1;
	}else if(Data > In->Data){
		Result = 1;
	}else{
		Result = 0;
	}
	return STAT_OK;
}

SSI_INT64 CDataBool::Register(SSI_INT64 NewType){
	sType = NewType;
	return 1;
}

void CDataBool::UnRegister(){
	sType = 0;
}



